package com.fs.controller;

import com.fs.pojo.Rta;
import com.fs.pojo.Sto;

import com.fs.pojo.WhInvInventoryDetail;
import com.fs.service.ProcurementService;
import com.fs.service.RtaService;
import com.fs.util.ResponseDataUtil;
import com.fs.vo.Condition;
import com.fs.vo.Meta;
import com.fs.vo.ResponseData;
import com.fs.vo.ResultEnums;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 刘伟
 * @fileName ProcurementController
 * @date 2024/10/15 9:41
 * @description
 **/
@RestController
@RequestMapping("/procurement")
public class ProcurementController {

    @Resource
    ProcurementService procurementService;
    @Resource
    private RtaService rtaService;

    /**
     * 获取采购清单列表
     *
     * @param condition
     * @return
     */
    @PostMapping("/list")
    public ResponseData<PageInfo<Sto>> getProcurementList(@RequestBody Condition<Sto> condition) {
        ResponseData<PageInfo<Sto>> res = new ResponseData<>();
        PageInfo<Sto> procurementList = procurementService.getProcurementList(condition);
        if (procurementList != null) {
            res.setData(procurementList);
            res.setMeta(new Meta(200, "请求成功"));
        } else {
            res.setMeta(new Meta(500, "请求错误"));
        }
        return res;
    }

    /**
     * 添加单条数据
     *
     * @param sto
     * @return
     */
    @PostMapping
    public ResponseData<Integer> addProcurement(@RequestBody Sto sto) {
        sto.setStoProId(null);
        ResponseData<Integer> res = new ResponseData<>();
        Integer i = procurementService.addProcurement(sto);
        if (i == 1) {
            res.setData(i);
            res.setMeta(new Meta(200, "添加成功"));
        } else {
            res.setMeta(new Meta(500, "添加失败"));
        }
        return res;
    }

    /**
     * 根据传入对象的主键修改对象
     *
     * @param sto
     * @return
     */
    @PutMapping
    public ResponseData<Integer> updateProcurement(@RequestBody Sto sto) {
        System.out.println(sto);
        ResponseData<Integer> res = new ResponseData<>();
        Integer i = procurementService.updateProcurement(sto);
        if (i == 1) {
            res.setData(i);
            res.setMeta(new Meta(200, "修改成功"));
        } else {
            res.setMeta(new Meta(500, "修改失败"));
        }
        return res;
    }

    /**
     * 根据id删除采购单
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseData<Integer> deleteProcurement(@PathVariable Integer id) {
        ResponseData<Integer> res = new ResponseData<>();
        Integer i = procurementService.deleteProcurement(id);
        if (i == 1) {
            res.setData(i);
            res.setMeta(new Meta(200, "删除成功"));
        } else {
            res.setMeta(new Meta(500, "删除失败"));
        }
        return res;
    }

    /**
     * 根据id查询单条对象
     *
     * @param id
     * @return
     */
    @GetMapping("/getStoById/{id}")
    public ResponseData<Sto> getProcurementById(@PathVariable Integer id) {
        ResponseData<Sto> res = new ResponseData<>();
        Sto sto = procurementService.queryProcurementById(id);
        if (sto != null) {
            res.setData(sto);
            res.setMeta(new Meta(200, "请求成功"));
        } else {
            res.setMeta(new Meta(500, "请求失败"));
        }
        return res;
    }

    /**
     * 获取退货申请单集合
     *
     * @param condition
     * @return
     */

    @PostMapping("/rta/list")
    public ResponseData<PageInfo<Rta>> findAllRta(@RequestBody Condition<Rta> condition) {
        return new ResponseData<>(ResultEnums.OK, rtaService.selectAll(condition));
    }

    /**
     * 删除退货单
     *
     * @param id
     * @return
     */
    @DeleteMapping("/rta/delRta/{id}")
    public ResponseData deleteRta(@PathVariable Integer id) {
        if (rtaService.deleteRta(id)) {
            return new ResponseData<>(ResultEnums.OK);
        } else return new ResponseData<>(ResultEnums.FAIL);
    }


    /**
     * 修改退货单
     *
     * @param condition
     * @return
     */
    @PutMapping("/rta/updateRta")
    public ResponseData updateRta(@RequestBody Condition<Rta> condition) {
        if (rtaService.updateRta(condition)) {
            return new ResponseData<>(ResultEnums.OK);
        } else return new ResponseData<>(ResultEnums.FAIL);
    }
    /**
     * 根据ID获取对象
     *
     * @param id
     * @return
     */
    @GetMapping("/rta/ById/{id}")
    public ResponseData<Rta> selectDetailById(@PathVariable Integer id) {
        return new ResponseData<>(ResultEnums.OK, rtaService.selectById(id));
    }

    @GetMapping("/maxId")
    public ResponseData<Integer> getMaxProcId(){
        Integer maxProId = procurementService.getMaxProId();
        return maxProId != null? ResponseDataUtil.buildOk(200,"操作成功",maxProId):ResponseDataUtil.buildFail(null);
    }
}
